package com.sojson.controller.po;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.AuthcParamVerify;
import com.sojson.entity.po.Member;
import com.sojson.enums.core.ELoginDeviceType;
import com.sojson.enums.core.ERoleType;
import com.sojson.result.Result;
import com.sojson.service.AuthcService;
import com.sojson.util.StringUtil;
import com.sojson.util.enums.ELoginOtherType;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.TokenVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 认证Controller
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "认证管理")
@RestController
@RequestMapping("/authc")
public class AuthcController {

    @Autowired
    private AuthcService authcService;

    /**
     * 帐号密码注册
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PostMapping("/pwdRegister")
    @PreAuthorize("@ss.hasPermi('/authc/pwdRegister')")
    @ApiOperation(value = "密码注册")
    public Object pwdRegister(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.pwdRegister(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Member member = token.getMember();
        member.setWxUnionid(null);
        // 注册用户
        boolean pwdRegister = authcService.pwdRegister(token);

        // 返回结果
        return pwdRegister ? Result.success() : Result.fail();
    }

    /**
     * 管理员密码登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PutMapping("/pwdLogin")
    @ApiOperation(value = "密码登录", response = String.class)
    public Object pwdLogin(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.pwdLogin(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Set<String> hashSet = new HashSet<>();
        hashSet.add(ERoleType.ADMIN.getKey());
        hashSet.add(ERoleType.COMMON.getKey());
        token.setRoles(hashSet);
        token.setLoginType(ELoginOtherType.GENERAL.getCode());
        token.setTypeLoginDevice(ELoginDeviceType.COMPUTER.getCode());
        authcService.pwdLogin(token);
        // 返回结果
        return Result.success(TokenUtil.getClaimsStr());
    }

    /**
     * 中介密码登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PutMapping("/pwdLoginApp")
    @ApiOperation(value = "中介密码登录", response = String.class)
    public Object pwdLoginApp(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.pwdLoginApp(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Set<String> hashSet = new HashSet<>();
        hashSet.add(ERoleType.LABOR.getKey());
        token.setRoles(hashSet);
        token.setLoginType(ELoginOtherType.GENERAL.getCode());
        token.setTypeLoginDevice(ELoginDeviceType.CELLPHONE.getCode());
        authcService.pwdLogin(token);
        // 返回结果
        return Result.success(TokenUtil.getClaimsStr());
    }

    /**
     * 商家密码登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PutMapping("/merchantLogin")
    @ApiOperation(value = "商家密码登录", response = String.class)
    public Object merchantLogin(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.pwdLoginApp(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Set<String> hashSet = new HashSet<>();
        hashSet.add(ERoleType.MERCHANT.getKey());
        token.setRoles(hashSet);
        token.setLoginType(ELoginOtherType.GENERAL.getCode());
        token.setTypeLoginDevice(ELoginDeviceType.CELLPHONE.getCode());
        authcService.pwdLogin(token);
        // 返回结果
        return Result.success(TokenUtil.getClaimsStr());
    }

    /**
     * 短信登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PutMapping("/smsLogin")
    @ApiOperation(value = "短信登录", response = String.class)
    public Object smsLogin(@RequestBody TokenVo token) throws Exception {
        // 参数初始化
        token.setLoginType(ELoginOtherType.MOBILE.getCode());
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.smsLogin(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Set<String> hashSet = new HashSet<>();
        hashSet.add(ERoleType.USER.getKey());
        token.setRoles(hashSet);
        token.setRoleId(ERoleType.USER.getCode());
        token.setLoginType(ELoginOtherType.MOBILE.getCode());
        token.setTypeLoginDevice(ELoginDeviceType.CELLPHONE.getCode());
        authcService.smsLogin(token);
        // 返回结果
        return Result.success(TokenUtil.getClaimsStr());
    }

    /**
     * 微信小程序登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    @PutMapping("/wxXcxLogin")
    @ApiOperation(value = "微信小程序登录", response = String.class)
    public Object wxXcxLogin(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = AuthcParamVerify.wxXcxLogin(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Set<String> hashSet = new HashSet<>();
        hashSet.add(ERoleType.USER.getKey());
        token.setRoles(hashSet);
        token.setRoleId(ERoleType.USER.getCode());
        token.setLoginType(ELoginOtherType.WX.getCode());
        token.setTypeLoginDevice(ELoginDeviceType.CELLPHONE.getCode());
        authcService.wxXcxLogin(token);
        // 返回结果
        return Result.success(TokenUtil.getClaimsStr());
    }

    /**
     * 移动应用绑定微信
     * 
     * @param code 用户授权后的code
     * @return
     * @throws Exception 
     */
    @PutMapping("/wxAppBindOpenid/{code}")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "移动应用绑定微信", response = int.class)
    public Object wxAppBindOpenid(@PathVariable String code) throws Exception {
        if (StringUtil.isBlankObject(code)) {
            return Result.fail("code不能为空！");
        }

        // 返回结果
        return Result.success(authcService.wxAppBindOpenid(code));
    }

    /**
     * 小程序绑定微信
     * 
     * @param code 用户授权后的code
     * @return
     * @throws Exception 
     */
    @PutMapping("/wxXcxBindOpenid/{code}")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "小程序绑定微信", response = int.class)
    public Object wxXcxBindOpenid(@PathVariable String code) throws Exception {
        if (StringUtil.isBlankObject(code)) {
            return Result.fail("code不能为空！");
        }

        // 返回结果
        return Result.success(authcService.wxXcxBindOpenid(code));
    }

    /**
     * 退出登录
     * 
     * @return
     * @throws Exception
     */
    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Object logout() throws Exception {
        TokenUtil.logout();

        // 返回结果
        return Result.success();
    }

}